package com.heima.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.domain.dto.ExamineDTO;
import com.heima.domain.dto.ReviewDTO;
import com.heima.domain.pojo.Review;
import com.heima.domain.pojo.Text;
import com.heima.domain.query.ReviewQueryParam;
import com.heima.domain.vo.PageVo;
import com.heima.mapper.ReviewMapper;
import com.heima.service.ReviewService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class ReviewServiceImpl extends ServiceImpl<ReviewMapper, Review> implements ReviewService {
    @Override
    public PageVo pageList(ReviewQueryParam reviewQueryParam) {
        PageHelper.startPage(reviewQueryParam.getPage(),reviewQueryParam.getPageSize());
        List<Review> textList = baseMapper.pageList(reviewQueryParam);
        Page<Review> page = (Page<Review>) textList;
        return new PageVo<>(page.getTotal(),page.getResult());
    }

    @Override
    public Review add(ReviewDTO dto) {
        Review review = new Review();
        review.setNub("IQC" + + System.currentTimeMillis() + "" + new Random().nextInt(9999));
        review.setLogisticsNumb(dto.getLogisticsNumb());
        review.setStatus(1);
        review.setCheckSchemaId(dto.getCheckSchemaId());
        review.setResult(null);
        review.setExecutor(dto.getExecutor());
        review.setExecuteTime(new Date());
        review.setReviewTime(null);
        review.setReview(null);
        review.setReviewTime(null);
        review.setReason(null);
        review.setCreateBy("admin");
        review.setCreateTime(new Date());
        baseMapper.insert(review);
        return review;
    }

    @Override
    public Review examine(ExamineDTO dto) {
        Review review = getById(dto.getId());
        if (ObjectUtils.isEmpty(review)){
            throw new RuntimeException("数据不存在");
        }

        review.setReview(dto.getReview());
        review.setReviewTime(new Date());
        review.setResult(dto.getResult());
        review.setReason(dto.getReason());
    baseMapper.updateById(review);
return review;
    }
}
